#include "StdAfx.h"
#include "IcvtColor.h"
//OpenCV libs
#include "opencv2/core/core.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv/cv.h"

const char * const IcvtColor::FunctionNameIcvtColor = "cvtColor";

IcvtColor::IcvtColor(void) 
{
	_fnApi.SetFunctionName(IcvtColor::FunctionNameIcvtColor);
	_fnApi.SetFunctionDescription("Converts image from one color space to another. Codes: to L*U*V* = CV_BGR2Luv; to Grey = CV_RGB2GRAY; For more codes, refer to http://opencv.willowgarage.com/documentation/cpp/miscellaneous_image_transformations.html ");
	
									//inputs
	_fnApi.AddParamDef(new ProcParamDefInt(0, "Code", "The color space conversion code", ProcParamDef::input, 0, 500 ));
	_fnApi.AddParamDef(new ProcParamDefInt(1, "Channel", "The number of channels in the destination image; if the parameter is 0, the number of the channels will be derived automatically from src and the code", ProcParamDef::input, 0, 500 ));
									//outputs
	_fnApi.AddParamDef(new ProcParamDefInt(0, ProcParam::attribOutputParamNameResult, "0 is success", ProcParamDef::output, 0, 1));

	_fnApi.Validate();
}


IcvtColor::~IcvtColor(void)
{

}


bool IcvtColor::Process(ImageProcStep *step, bool DebugMode)
{	
	bool rc = false;
	if ((step != '\0') && (step->IsValid()))
	{
		
		ProcParamInt *code = dynamic_cast<ProcParamInt *>(step->GetInputs()->GetParam(0));	//Check the Param number
		ProcParamInt *channel = dynamic_cast<ProcParamInt *>(step->GetInputs()->GetParam(1));
		//ProcParamInt *result = dynamic_cast<ProcParamInt *>(step->GetOutputs()->GetParam(0));		//Check the type and check the DefType
		if  (code != '\0')
		{
			cv::Mat output;
			cv::Mat input = cv::imread(step->GetIntermediateImagePathFilename());
			
			cv::cvtColor(input, output, int(code->GetValue()), int(channel->GetValue()));

			cv::imwrite(step->GetIntermediateImagePathFilename(), output);

			input.release();
			//result->SetValue(0);
			rc = true;
		}
	}
	return rc;
}
